package com.ruoyi.web.controller.contest.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.activity.service.IContestService;
import com.ruoyi.common.annotation.Check;
import com.ruoyi.common.annotation.RequestLock;
import com.ruoyi.common.constant.ActivitysConstant;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.contest.Contest;
import com.ruoyi.model.contest.dto.ContestDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author : 张怀秋
 **/
@Api(tags = "前台:竞赛接口")
@RestController
@RequestMapping("/api/contest")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ContestApiController extends BaseController {
    private final IContestService contestService;

    @ApiOperation("发布竞赛")
    @Check(isCheckOrganization = true)
    @PostMapping("/push/contest")
    @RequestLock(prefix = "pushContest", expire = 1, timeUnit = TimeUnit.SECONDS)
    public AjaxResult pushContest(@ApiParam("发布竞赛的信息") @RequestBody ContestDto contestDto) {
        return toAjax(contestService.pushContest(contestDto));
    }

    @ApiOperation(value = "查询竞赛详情", notes = "并且返回对应的评论列表,并且增加对应的浏览量")
    @GetMapping("/getContestInfo/{id}/{type}")
    public AjaxResult getContestInfo(@ApiParam("竞赛Id") @PathVariable("id") String contestId,
                                     @ApiParam("类型") @PathVariable String type) {
        return success(this.contestService.getContestInfoAndCommentList(contestId, type));
    }

    @ApiOperation("根据子分类查询竞赛")
    @GetMapping("/queryContestListByCategoryId/{categoryId}")
    public AjaxResult queryContestListByCategoryId(@PathVariable String categoryId) {
        return AjaxResult.success(this.contestService.list(Wrappers.<Contest>lambdaQuery()
                        .eq(Contest::getCategoryId, categoryId)
                        .eq(Contest::getStatus, "0")
                        .orderByDesc(Contest::getViewNum)).stream().limit(8)
                .collect(Collectors.toList()));
    }

    @ApiOperation("查询竞赛列表")
    @GetMapping("/contest-list/{page}/{limit}")
    public AjaxResult deskContestList(@ApiParam("当前页码") @PathVariable Long page,
                                      @ApiParam("每页显示条数") @PathVariable Long limit,
                                      @ApiParam("分类id") String categoryId) {
        return AjaxResult.success(this.contestService.deskContestList(page, limit, categoryId));
    }

    @ApiOperation(value = "当前最热门竞赛前10条", notes = "根据浏览量排序")
    @GetMapping("/contest-hotByOrder")
    public AjaxResult hotByOrderContest() {
        return AjaxResult.success(this.contestService.list(Wrappers.<Contest>lambdaQuery()
                .eq(Contest::getStatus, "0")
                .orderByDesc(Contest::getViewNum).last("LIMIT 10")).stream());
    }

    @ApiOperation(value = "当前最新竞赛前10条", notes = "根据发布时间排序")
    @GetMapping("/contest-newByOrder")
    public AjaxResult newByOrderContest() {
        return AjaxResult.success(this.contestService.list(Wrappers.<Contest>lambdaQuery()
                .eq(Contest::getStatus, "0")
                .orderByDesc(Contest::getPushTime).last("LIMIT 10")));
    }

}
